package com.xjrsoft.module.oa.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.StringPool;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.oa.dto.OaScheduleDto;
import com.xjrsoft.module.oa.entity.XjrOaSchedule;
import com.xjrsoft.module.oa.service.IXjrOaMessageService;
import com.xjrsoft.module.oa.service.IXjrOaScheduleService;
import com.xjrsoft.module.oa.utils.SendMessageUtil;
import com.xjrsoft.module.oa.vo.OaScheduleListVo;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 日程管理 前端控制器
 * </p>
 *
 * @author zwq
 * @since 2021-07-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("/oa-schedule")
public class XjrOaScheduleController {

    private final IXjrOaScheduleService scheduleService;

    private final IXjrOaMessageService messageService;


    @GetMapping
    @ApiOperation(value = "获取日程管理列表")
    public Response<List<OaScheduleListVo>> getList() {
        List<XjrOaSchedule> scheduleList = scheduleService.list(Wrappers.<XjrOaSchedule>query().lambda()
                .eq(XjrOaSchedule::getEnabledMark, 1)
                .eq(XjrOaSchedule::getCreateUserId, SecureUtil.getUserId())
                .orderByAsc(XjrOaSchedule::getSortCode));
        List<OaScheduleListVo> resultList = new ArrayList<>(scheduleList.size());
        for (XjrOaSchedule schedule : scheduleList) {
            OaScheduleListVo scheduleListVo = new OaScheduleListVo();
            scheduleListVo.setId(schedule.getScheduleId());
            scheduleListVo.setTitle(schedule.getScheduleContent());
            String endTime = schedule.getEndTime();
            if (!StringUtil.isEmpty(endTime)) {
                String[] endTimeArray = StringUtils.split(endTime, ":");
                scheduleListVo.setEnd(schedule.getEndDate().withHour(Integer.valueOf(endTimeArray[0])).withMinute(Integer.valueOf(endTimeArray[1])).withSecond(Integer.valueOf(endTimeArray[2])));
            }
            String startTime = schedule.getStartTime();
            if (!StringUtil.isEmpty(startTime)) {
                String[] startTimeArray = StringUtils.split(startTime, ":");
                scheduleListVo.setStart(schedule.getStartDate().withHour(Integer.valueOf(startTimeArray[0])).withMinute(Integer.valueOf(startTimeArray[1])).withSecond(Integer.valueOf(startTimeArray[2])));
            }
            scheduleListVo.setAllDay(false);
            resultList.add(scheduleListVo);
        }
        return Response.ok(resultList);
    }

    @PostMapping
    @ApiOperation(value = "添加日程管理")
    public Response save(@RequestBody OaScheduleDto scheduleDto) {
        XjrOaSchedule schedule = BeanUtil.copy(scheduleDto, XjrOaSchedule.class);
        //添加消息通知
        boolean save = scheduleService.save(schedule);
        SendMessageUtil.sendOaScheduleMessage(schedule);
        return Response.status(save);
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "修改日程管理")
    public Response update(@PathVariable String id, @RequestBody OaScheduleDto scheduleDto) {
        XjrOaSchedule schedule = BeanUtil.copy(scheduleDto, XjrOaSchedule.class);
        schedule.setScheduleId(id);
        return Response.status(scheduleService.updateById(schedule));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除日程管理")
    public Response delete(@PathVariable String id) {
        String[] idArray = StringUtils.split(id, StringPool.COMMA);
        boolean isSuccess = false;
        if (idArray.length == 1) {
            isSuccess = scheduleService.removeById(idArray[0]);
            messageService.deleteNewsMessage(idArray[0]);
        } else {
            isSuccess = scheduleService.removeByIds(Arrays.asList(idArray));
            for (String scheduleId : idArray) {
                messageService.deleteNewsMessage(scheduleId);
            }
        }
        return Response.status(isSuccess);
    }
}
